package com.example.s.fragment.main_child_fragment.discovery_child_fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.s.Activity.UserDetailActivity;
import com.example.s.Activity.WorksDetailActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.bean.LoopImg;
import com.example.s.bean.RecContent;
import com.example.s.fragment.BaseFragment;
import com.example.s.network.OnMyNetworkListener;
import com.example.s.utils.LiveDataBus;
import com.example.s.utils.MyUtils;
import com.example.s.view.MyScrollView;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RecommandFragment extends BaseFragment {
    private Banner banner;
    private List<LoopImg> bannerData = new ArrayList<>();
    private RecyclerView rv;
    private MyRecyclerViewAdapter<RecContent> mContentMyRecyclerViewAdapter;
    private List<RecContent> mRecContentList = new ArrayList<>();
    private MyScrollView sv;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_recommand;
    }

    @Override
    public void initView(View view) {
        banner = (Banner) findViewById(R.id.banner);
        rv = (RecyclerView) findViewById(R.id.rv);
        sv = (MyScrollView) findViewById(R.id.sv);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

//        bannerData.clear();
//
//
//        bannerData.add(R.drawable.yywlb1);
//        bannerData.add(R.drawable.yywlb2);
//        bannerData.add(R.drawable.yywlb3);
//        bannerData.add(R.drawable.yywlb4);
//        bannerData.add(R.drawable.yywlb5);
        banner.setBannerRound(MyUtils.getInstance().dip2px(getContext(), 13));
        banner.setAdapter(new BannerImageAdapter<LoopImg>(bannerData) {
            @Override
            public void onBindView(BannerImageHolder holder, LoopImg data, int position, int size) {
                loadImg(holder.imageView, data.getImg_url());
            }
        })
                .addBannerLifecycleObserver(getActivity())
                .setIndicator(new CircleIndicator(getContext()))
                .setLoopTime(5000);

        /**
         * 获取轮推荐播图数据
         */
        getLoopImg();

        getRecItemData();
        rv.setNestedScrollingEnabled(false);
        rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        rv.setAdapter(mContentMyRecyclerViewAdapter = new MyRecyclerViewAdapter<RecContent>(mRecContentList, R.layout.rec_recycler_item) {
            @Override
            public void handleData(InnerHolder holder, RecContent item) {

//                new Broccoli().addPlaceholders((ViewGroup) holder.itemView,R.id.headphoto,R.id.title,R.id.subtitle,R.id.likes,R.id.contentPhotoContainer).show();
                holder.setSrcResource(R.id.headphoto, item.getHeadPhoto()).setSrcResource(R.id.contentPhoto, item.getContentPhoto());
                holder.setText(R.id.title, item.getTitle());
                holder.setText(R.id.subtitle, item.getSubTitle());
                holder.setText(R.id.likes, String.valueOf(item.getLikes()));
                holder.setOnClickListener(R.id.headphoto, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LiveDataBus.getInstance().with("currentUser", RecContent.class).setValue(item);
                        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
                        startActivity(intent);
                    }
                });
                holder.setOnClickListener(R.id.contentPhotoContainer, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LiveDataBus.getInstance().with("currentWorkDetail", RecContent.class).setValue(item);
                        Intent intent = new Intent(getActivity(), WorksDetailActivity.class);
                        startActivity(intent);
                    }
                });


            }
        });

        sv.setOnScrollToTopOrBottomListener(new MyScrollView.OnScrollToTopOrBottomListener() {
            @Override
            public void toTop() {
//                String rec = listener.onRefresh("rec").isEmpty() ? "" : listener.onRefresh("rec");
//                Log.d(TAG, "toTop: 父fragment 传入 子fragment的数据" + rec);
            }

            @Override
            public void toBottom() {
//                listener.onLoad("rec");

            }

        });
//        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                Log.d(TAG, "--------------------------------------");
//
////达到这个条件就说明滑到了顶部
//
//                StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
//                int[] firstV = layoutManager.findFirstVisibleItemPositions(null);
//                if (recyclerView.getChildAt(0).getY() == 0f && firstV[0] == 0) {
//                    Log.d(TAG, "滑动到顶部");
//                }
//                int[] lastPositions = new int[layoutManager.getSpanCount()];
//                layoutManager.findLastVisibleItemPositions(lastPositions);
//                int lastPosition = findMax(lastPositions);
//                if (lastPosition == recyclerView.getLayoutManager().getItemCount() - 1) {
//                    Log.d(TAG, "滑动到底部");
//                }
//
//            }
//
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//            }
//        });

    }

    private void getLoopImg() {
        N.get("/api/loop", new OnMyNetworkListener() {
            @Override
            public void success(JSONObject response) {
                bannerData.clear();
                bannerData.addAll(LoopImg.arrayLoopImgFromData(response.optJSONObject("data").optString("data")));
                LoopImg.saveInTx(bannerData);
            }

            @Override
            public void fail(JSONObject response) {
                bannerData.addAll(LoopImg.listAll(LoopImg.class));

            }
        });
    }

    //找到数组中的最大值
//    private int findMax(int[] lastPositions) {
//        int max = lastPositions[0];
//        for (int value : lastPositions) {
//            if (value > max) {
//                max = value;
//            }
//        }
//        return max;
//    }


    private int getX() {
        int x = (int) (Math.random() * 200);
        return x;
    }

    private void getRecItemData() {
        //"一个字就是牛","居然可以这么做","笑掉大牙","不愧是我","艺高人胆大","快乐的亚索","采访一下","新人演唱","小可爱","绝对不是这个姿势","datang","帅到爆炸","中国古典乐器","不动刀!整容级变美","新歌分享","cos美美","我又通关了","帅哥","给大家分享美食","何同学","果果"
        // ,"想画这种妆容","fight","孤独","???","专业玩家","蓝屏人物?","分享乐曲","美食分享","教你做菜","鉴定","亲子旅游","原创冰棍","煎蛋","揉???","我的世界,拜年来了","黑客","这是啥?"

//        "新年成为一个大牛","满脸的问号????","笑掉大牙的操作","不愧是我,居然可以这么美","胆子大游戏里也可以冲","面对疾风吧,哈撒给",
//                "大爷你好厉害","新人演唱海底,绝了","可可爱爱的","姿势帅才行","不要以为我是猪","..........","中华传承,古韵犹存",
//                "亚洲四大邪术ps","大家点赞支持一下","美如画cos","游戏而已","唱歌真好听","教大家做菜,一道美味的食物","挑战一下",
//                "好吃","美美出境","冲冲冲","孤独的人生","????","这就是专业","爱了爱了","乐曲","美食分享","教你做菜","鉴定一下",
        //  "今天去外地旅游,带上家人一起", "椰子牌雪糕", "简单的煎蛋", "揉一揉", "新年好", "冷血杀手", "啥也不是"

        mRecContentList.add(new RecContent(1, R.drawable.yywtx1, "一个字就是牛", R.drawable.yyw1, "新年成为一个大牛", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx2, "居然可以这么做", R.drawable.yyw2, "满脸的问号", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx3, "笑掉大牙", R.drawable.yyw3, "笑掉大牙的操作", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx4, "不愧是我", R.drawable.yyw4, "不愧是我,居然可以这么美", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx5, "艺高人胆大", R.drawable.yyw5, "胆子大游戏里也可以冲", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx6, "快乐的亚索", R.drawable.yyw6, "面对疾风吧,哈撒给", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx7, "采访一下", R.drawable.yyw7, "大爷你好厉害", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx8, "新人演唱", R.drawable.yyw8, "新人演唱海底,绝了", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx9, "小可爱", R.drawable.yyw9, "可可爱爱的", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx10, "绝对不是这个姿势", R.drawable.yyw10, "姿势帅才行", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx11, "datang", R.drawable.yyw11, "不要以为我是猪", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx12, "帅到爆炸", R.drawable.yyw12, "..........", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx13, "中国古典乐器", R.drawable.yyw13, "中华传承,古韵犹存", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx14, "不动刀!整容级变美", R.drawable.yyw14, "亚洲四大邪术ps", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx15, "新歌分享", R.drawable.yyw15, "大家点赞支持一下", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx16, "cos美美", R.drawable.yyw16, "美如画cos", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx17, "我又通关了", R.drawable.yyw17, "游戏而已", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx18, "帅哥", R.drawable.yyw18, "唱歌真好听", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx19, "给大家分享美食", R.drawable.yyw19, "教大家做菜,一道美味的食物", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx20, "何同学", R.drawable.yyw20, "挑战一下", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx21, "果果", R.drawable.yyw21, "好吃", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx22, "想画这种妆容", R.drawable.yyw22, "美美出境", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx23, "fight", R.drawable.yyw23, "冲冲冲", getX(), 666));
        mRecContentList.add(new RecContent(1, R.drawable.yywtx24, "孤独", R.drawable.yyw24, "孤独的人生", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx25, "???", R.drawable.yyw25, "????????", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx26, "专业玩家", R.drawable.yyw26, "这就是专业", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx27, "蓝屏人物", R.drawable.yyw27, "爱了爱了", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx28, "分享乐曲", R.drawable.yyw28, "乐曲", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx29, "美食分享", R.drawable.yyw29, "美食分享", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx30, "教你做菜", R.drawable.yyw30, "教你做菜", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx31, "鉴定", R.drawable.yyw31, "鉴定一下", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx1, "亲子旅游", R.drawable.yyw32, "今天去外地旅游,带上家人一起", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx2, "原创冰棍", R.drawable.yyw33, "椰子牌雪糕", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx3, "煎蛋",R.drawable.yyw34, "简单的煎蛋", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx4, "揉???", R.drawable.yyw35, "揉一揉", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx5, "我的世界,拜年来了", R.drawable.yyw36, "新年好", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx6, "黑客", R.drawable.yyw37, "冷血杀手", getX(), 666));
//        mRecContentList.add(new RecContent(1, R.drawable.yywtx7, "这是啥？", R.drawable.yyw38, "啥也不是", getX(), 666));
    }

}
